<?php

namespace app\controller\admin\lineup;

use app\model\admin\lineup\LineupGame;
use app\model\admin\lineup\LineupStyleLabelRelation;
use app\model\admin\lineup\LineupTeam;
use laytp\controller\Backend;
use think\facade\Db;

/**
 * Class Career
 * @package app\controller\admin\ah
 */
class TeamAudit extends Backend
{
    protected $model;
    protected $noNeedAuth = ['index', 'info', 'audit'];
    protected $type = 1;
    protected static $audit_status_name = [
        1 => "待审核", 2 => "审核通过", 3 => "审核不通过"
    ];

    public function initialize()
    {
        $this->model = new LineupTeam();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 列表
     *  all_data参数的值为true时，表示查询表中所有数据集，否则进行分页查询
     * @return mixed
     */
    public function index()
    {
        $where = $this->buildSearchParams();
        $order = $this->buildOrder();
        $allData = $this->request->param('all_data');
        if ($allData) {
            $data = $this->model->where($where)->order($order)->cache(true)->select()->toArray();
        } else {
            $limit = $this->request->param('limit', 10);
            $data = $this->model->with("style")->where($where)->order($order)->paginate($limit)->toArray();
            foreach ($data['data'] as &$item) {
                $item['audit_status_name'] = self::$audit_status_name[$item['audit_status']];
            }
        }
        return $this->success('数据获取成功', $data);
    }

    public function info()
    {
        $id = $this->request->param('id');
        $info = $this->model->find($id);
        if (is_object($info)) $info = $info->toArray();
        $list = (new LineupStyleLabelRelation())->where("lineup_style_id", $id)->select()->toArray();
        $relations = [];
        foreach ($list as $item) {
            $relations[$item['type']][] = $item['label_id'];
        }
        foreach ($relations as $key => $item) {
            $info['relation_ids_' . $key] = implode(",", $item);
        }
        $info['basic_game_id'] = (new LineupGame())->where('id', $info['lineup_game_id'])->value("basic_game_id");
        return $this->success('获取成功', $info);
    }

    //审核
    public function setStatus()
    {
        $id = $this->request->post('id');
        $fieldVal = $this->request->post('status');
        $update['audit_status'] = $fieldVal;
        try {
            if ($update['audit_status'] == 2) {
                $info = $this->model->find($id);
                $style = Db::name("lineup_style")->where("lineup_game_id", $info['lineup_game_id'])->where("status", 1)->find();
                if ($style) {
                    $update['game_power_number'] = $style['start_power_number'];
                }
            }
            $updateRes = $this->model->where('id', '=', $id)->update($update);
            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }

    public function audit()
    {
        $data = [];
        foreach (self::$audit_status_name as $key => $item) {
            $data[] = [
                'name' => $item, 'value' => $key
            ];
        }
        return $this->success('获取审核状态', $data);
    }
}